/* Janus Chat - Version 1.0
 * Authors: Matthew Perry
 *          Michael Freid
 *          Marc Latou
 *          Richard Lee
 *
 * The Janus Chat Client is a highly secured
 * peer-to-peer chat client designed to facilitate
 * the transfer of sensitive data or as a communication
 * medium for the extra paranoid
 *
 * This file contains the implementation for the ServerTab
 * QT class which facilitates the input of data for server
 * connections */

#include "servertab.h"
#include "connecttab.h"
#include "chattab.h"
#include "ui_servertab.h"
#include "../lib/exceptions.h"
#include <QRegExp>

#include <QDebug>

/* The ServerTab constructor
 * This constructor initializes the UI,
 * connects some signals and slots and
 * installs the enter key filter to its
 * inputs */
ServerTab::ServerTab(User* user, QWidget *parent) :
    DetailTab(user, parent),
    ui(new Ui::ServerTab)
{
    //Initialize all of the UI Elements (Widgets) for this class
    ui->setupUi(this);

    //Establish a signal and slot connection for the buttons on this class
    connect( ui->buttonOk, SIGNAL(clicked()), this, SLOT(buttonOk_clicked()) );
    connect( ui->buttonBack, SIGNAL(clicked()), this, SLOT(buttonBack_clicked()) );

    //Install an event filter for pressing the enter button
    ui->lineIP->installEventFilter( this );
    ui->linePort->installEventFilter( this );
    ui->lineName->installEventFilter( this );
    ui->linePWord->installEventFilter( this );
}

/* Destructor */
ServerTab::~ServerTab()
{
    delete ui;
}

/* The connectSocket function:
 * This function is an override of the pure
 * virtual function from the DetailTab class
 * it simply describes a means to connect through a server */
void ServerTab::connectSocket( QTcpSocket* sock )
{
    //First establish a basic connection to the server
    sock->connectToHost( ui->lineIP->text(), ui->linePort->text().toInt() );
}

/* The getAddress function:
 * Simply gives the detailtab super class
 * the ability to see the address that the
 * user enters */
QString ServerTab::getAddress()
{
    return ui->lineIP->text();
}

/* The beginChat function:
 * Simply gives the detailtab super class
 * the ability to start a new server chat */
void ServerTab::beginChat()
{
    //Send a request to the server with the given name/pword
    Message msg( Message::ServerRequest );
    msg.addField( ui->lineName->text().toStdString() );
    msg.addField( ui->linePWord->text().toStdString() );
    mySock->write( msg.toChar(), Message::MAX_SIZE );
    if( mySock->waitForReadyRead() )
    {
        char buffer[ Message::MAX_SIZE ];
        int retval = mySock->read( buffer, Message::MAX_SIZE );
        Message msg2( buffer, (unsigned int)retval );
        qDebug() << QString::fromStdString( msg2[0] );
        qDebug() << QString::fromStdString( msg2[1] );
    }
}

/* the checkInput function:
 * This function is an override of the pure
 * virtual function from the DetailTab class
 * it ensures that the input is valid */
QString ServerTab::checkInput()
{
    return "Server Functionality is under construction.";

    QString retval;
    //Check IP
    QString ipAddr = ui->lineIP->text();
    if( ipAddr.isEmpty() )
        retval.append( "* You must enter an IP Address\n" );
    else
    {
        //Validate IP
        QRegExp ip( "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" );
        QRegExp host( "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z]|[A-Za-z][A-Za-z0-9-]*[A-Za-z0-9])$" );
        if( !ip.exactMatch( ipAddr ) && !host.exactMatch( ipAddr ) )
            retval.append( "* Invalid IP Address or Hostname\n" );
    }

    //Check Port
    QString portStr = ui->linePort->text();
    if( portStr.isEmpty() )
        retval.append( "* You must enter a Port Number\n" );
    else
    {
        //Validate Port
        bool isNumber;
        int port = portStr.toInt( &isNumber );
        if( !isNumber )
            retval.append( "* Invalid Port Number\n" );
        else
        {
            //Ensure Port is in a valid range
            if( port > 65535 )
                retval.append( "* Port Number must be less than 65535\n" );
        }
    }

    //Check Room Name
    QString roomName = ui->lineName->text();
    if( roomName.isEmpty() )
        retval.append( "* You must enter a Room Name\n" );
    else
    {
        //Do we need to validate the room name?
    }

    //Check Password
    QString password = ui->linePWord->text();
    if( password.isEmpty() )
        retval.append( "* You must enter a Password\n" );
    else
    {
        //Do we need to validate the password?
    }

    return retval;
}
